﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TimeQuest.Base
{
    public class AnimatedPictureItem : AnimatedPicture
    {
        #region Construtors

        public AnimatedPictureItem()
        {
            OnClick += new EventHandler(AnimatedPictureItem_OnClick);
        }

        #endregion

        #region Events

        void AnimatedPictureItem_OnClick(object sender, EventArgs e)
        {
            MainGame.thePlayer.InventoryItems.Add(
                new Item()
                {
                    ItemName = "Test Item",
                    TextureID = MainGame.InventoryItemTextureID,
                    TextureRectangle = new Rectangle(1, 1, 50, 50)
                });
            var tHis = MainGame.theWorld.CurrentScreen.GameObjects.Where(x => x.GameObjectID == this.GameObjectID).SingleOrDefault();
            MainGame.theWorld.CurrentScreen.GameObjects.Remove(tHis);
        }
        public override void Update(GameTime gt)
        {
            base.Update(gt);
            MouseState ms = Mouse.GetState();
            if (InputHandler.LeftClicked(ms) &&
                InputHandler.CurrentCursor == Cursors.Touch &&
                BaseRectangle.Within(new Point(ms.X, ms.Y)))
            {
                this.InvokeOnClick(null, null);
            }
        }

        #endregion

        #region Public Static Methods

        public static AnimatedPictureItem BuildTest(int textureID)
        {
            AnimatedPictureItem i = new AnimatedPictureItem();
            i.GameObjectTextureID = textureID;
            i.Frames.Add(new Frame() { TextureRectangle = new Rectangle(1, 1, 10, 10), ShowTime = 2.75f });
            i.Frames.Add(new Frame() { TextureRectangle = new Rectangle(12, 1, 10, 10), ShowTime = .1f });
            i.Frames.Add(new Frame() { TextureRectangle = new Rectangle(23, 1, 10, 10), ShowTime = .1f });
            i.Frames.Add(new Frame() { TextureRectangle = new Rectangle(12, 1, 10, 10), ShowTime = .1f });
            i.Width = i.Height = 10;
            i.Location = new Point(400, 400);
            i.BlocksPlayer = false;
            i.BaseWidth = i.BaseHeight = 10;
            return i;
        }

        #endregion
    }
    public class AnimatedPicture : GameObject
    {
        #region Properties

        #region Public

        public IList<Frame> Frames = new List<Frame>();
        public int Width = 0;
        public int Height = 0;
        public override int BaseHeight
        {
            get
            {
                return base.BaseHeight;
            }
            set
            {
                base.BaseHeight = value;
            }
        }
        public override int BaseWidth
        {
            get
            {
                return base.BaseWidth;
            }
            set
            {
                base.BaseWidth = value;
            }
        }

        #endregion

        #region Private

        int CurrentFrame = 0;
        double LastFrameSwitch { get; set; }

        #endregion

        #endregion

        public override void Update(GameTime gt)
        {
            base.Update(gt);
            if (gt.TotalGameTime.TotalSeconds - LastFrameSwitch > Frames[CurrentFrame].ShowTime)
            {
                LastFrameSwitch = gt.TotalGameTime.TotalSeconds;
                CurrentFrame++;
                if (CurrentFrame > Frames.Count - 1)
                    CurrentFrame = 0;
            }
        }
        public override void Draw(SpriteBatch sb)
        {
            Frames[CurrentFrame].Draw(sb, GameObjectTextureID, new Rectangle(Location.X, Location.Y, Width, Height));
            if (GameWorld.ShowBlockingRectangles)
            {
                sb.Draw(ContentHandler.GetTexture(3), BaseRectangle, Rectangle.Empty, new Color(Color.Red, .3f));
                sb.Draw(ContentHandler.GetTexture(3), new Rectangle(BaseRectangle.X, CenterOfBaseY, BaseRectangle.Width, 1), Rectangle.Empty, Color.Red);
            }
        }

        #region Public Static Methods

        public static AnimatedPicture BuildTest(int textureID)
        {
            AnimatedPicture p = new AnimatedPicture();
            p.BlocksPlayer = true;
            p.GameObjectTextureID = textureID;
            p.Frames.Add(new Frame() { TextureRectangle = new Rectangle(100, 0, 50, 50), ShowTime = .1f });
            p.Frames.Add(new Frame() { TextureRectangle = new Rectangle(50, 0, 50, 50), ShowTime = .1f });
            p.Frames.Add(new Frame() { TextureRectangle = new Rectangle(0, 0, 50, 50), ShowTime = .1f });
            p.Frames.Add(new Frame() { TextureRectangle = new Rectangle(50, 0, 50, 50), ShowTime = .1f });
            p.Location = new Point(110, 400);
            p.Width = p.Height = 50;
            p.BaseWidth = 30;
            p.BaseHeight = 10;
            p.BaseYoffset = 30;
            p.BaseXoffset = 10;
            return p;
        }

        #endregion
    }

    public class Frame
    {
        #region Properties

        public Rectangle TextureRectangle { get; set; }
        public double ShowTime { get; set; }

        #endregion

        internal void Draw(SpriteBatch sb, int TextureID, Rectangle Location)
        {
            sb.Draw(ContentHandler.GetTexture(TextureID),
                Location,
                TextureRectangle,
                Color.White);
        }
    }
}
